/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dto.mapping;

import cz.muni.fi.pa165.creatures.dto.RegionDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.api.MappingDTO;
import cz.muni.fi.pa165.creatures.model.Region;

/**
 * This class represents the mapping between
 * {@link cz.muni.fi.pa165.creatures.model.Region} and
 * {@link cz.muni.fi.pa165.creatures.dto.RegionDTO} and vice versa.
 *
 * @author smikloso
 */
public class RegionMapping implements MappingDTO<RegionDTO, Region> {

    @Override
    public RegionDTO entityToDTO(Region region) {
        if (region == null) {
            return null;
        }

        RegionDTO dto = new RegionDTO();

        if (region.getId() != null) {
            dto.setId(Long.toString(region.getId()));
        }

        if (region.getName() != null) {
            dto.setName(region.getName());
        }

        if (region.getDescription() != null) {
            dto.setDescription(region.getDescription());
        }

        if (region.getArea() != null) {
            dto.setArea(region.getArea());
        }

        return dto;
    }

    @Override
    public Region DTOtoEntity(RegionDTO dto) {
        if (dto == null) {
            return null;
        }

        Region region = new Region();
        if (dto.getId() != null) {
            region.setId(Long.parseLong(dto.getId()));
        }

        if (dto.getName() != null) {
            region.setName(dto.getName());
        }

        if (dto.getDescription() != null) {
            region.setDescription(dto.getDescription());
        }

        if (dto.getArea() != null) {
            region.setArea(dto.getArea());
        }

        return region;
    }
}
